<template>
	<view class="register-container">
		<view class="header">
			<text class="back" @tap="goBack">返回</text>
			<text class="title">注册账号</text>
		</view>

		<view class="form-box">
			<view class="input-group">
				<text class="iconfont icon-phone"></text>
				<input type="number" v-model="form.userName" placeholder="请输入用户名" maxlength="11" />
			</view>
			<view class="input-group">
				<text class="iconfont icon-phone"></text>
				<input type="number" v-model="form.phone" placeholder="请输入手机号" maxlength="11" />
			</view>
			<view class="input-group">
				<text class="iconfont icon-phone"></text>
				<input type="number" v-model="form.userAccount" placeholder="请输入账号" maxlength="11" />
			</view>
			<view class="input-group">
				<text class="iconfont icon-lock"></text>
				<input type="password" v-model="form.userPassword" placeholder="请设置密码" />
			</view>

			<view class="input-group">
				<text class="iconfont icon-lock"></text>
				<input type="password" v-model="form.confirmPassword" placeholder="请确认密码" />
			</view>

			<button class="register-btn" @tap="handleRegister">注册</button>

			<view class="agreement">
				<checkbox :checked="agreed" @tap="toggleAgreement" color="#07c160" />
				<text class="agreement-text">我已阅读并同意</text>
				<text class="link">《用户协议》</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		request
	} from '@/utils/request.js'
	export default {
		data() {
			return {
				form: {
					userName: '',
					phone: '',
					userAccount: '',
					userPassword: '',
					confirmPassword: ''
				},
				agreed: false,
				counting: false,
				countDown: 60
			}
		},
		methods: {
			goBack() {
				uni.navigateBack()
			},

			async handleRegister() {
				if (!this.form.userAccount || !this.form.userPassword || !this.form.confirmPassword || !this.form
					.userName || !this.form.phone) {
					uni.showToast({
						title: '请填写完整信息',
						icon: 'none'
					})
					return
				}
				if (!this.agreed) {
					uni.showToast({
						title: '请阅读并同意用户协议',
						icon: 'none'
					})
					return
				}
				if (this.form.userPassword !== this.form.confirmPassword) {
					uni.showToast({
						title: '两次密码输入不一致',
						icon: 'none'
					})
					return
				}
				const res = await request({
					url: '/user/register',
					method: 'post',
					data: this.form
				})
				if (res.code == 200) {
					uni.showToast({
						title: '注册成功',
						icon: 'success'
					})



					uni.navigateTo({
						url: '/pages/login/login'
					})
				} else if (res.code = 2101) {
					uni.showToast({
						title: '账号已存在',
						icon: 'none'
					})
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			toggleAgreement() {
				this.agreed = !this.agreed
			},
		},
	}
</script>

<style lang="scss">
	.register-container {
		min-height: 100vh;
		background: #fff;

		.header {
			margin-top: 40rpx;
			position: relative;
			height: 88rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			border-bottom: 1rpx solid #eee;

			.back {
				position: absolute;
				left: 30rpx;
				font-size: 28rpx;
				color: #666;
			}

			.title {
				font-size: 32rpx;
				font-weight: bold;
				color: #333;
			}
		}

		.form-box {
			padding: 40rpx;

			.input-group {
				display: flex;
				align-items: center;
				border-bottom: 1rpx solid #eee;
				padding: 20rpx 0;
				margin-bottom: 30rpx;

				.iconfont {
					font-size: 40rpx;
					color: #666;
					margin-right: 20rpx;
				}

				input {
					flex: 1;
					font-size: 28rpx;
				}
			}

			.verify-group {
				.verify-btn {
					font-size: 26rpx;
					color: #07c160;
					padding: 10rpx 20rpx;
					border: 1rpx solid #07c160;
					border-radius: 30rpx;

					&.disabled {
						color: #999;
						border-color: #999;
					}
				}
			}

			.register-btn {
				background: #07c160;
				color: #fff;
				border-radius: 40rpx;
				margin: 40rpx 0;
				font-size: 32rpx;

				&:active {
					opacity: 0.8;
				}
			}

			.agreement {
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 26rpx;
				color: #666;

				.agreement-text {
					margin: 0 10rpx;
				}

				.link {
					color: #07c160;
				}
			}
		}
	}
</style>